<?

if (!permitted('edit', $node = (int)param('node'))) {
	header("Location: "._transformURL($CURRENT->url, array('remove_params' => 'all', 'add_param' => "node:$node")));
	exit;
}


$DATA['components'] = array();

if (($temp = @glob($_SERVER['DOCUMENT_ROOT'].FP_COMPONENT_PATH.'*', GLOB_ONLYDIR)) && sizeof($temp)) {
	foreach ($temp as $t)
		$DATA['components'][] = substr(strrchr($t, "/"), 1);
}

if (($c = (int)param('component')) && ($temp = datafetch('db_item', tb('data'), "id_data=".D_MULTIBLOCK." AND id_parent in (".$DATA['page']['id'].", 0) AND id=$c")) && in_array($temp['value'], $DATA['components'])) {


	
	if (!($temp['id_parent'] || permitted('configure', $node))) {
		header("Location: "._transformURL($CURRENT->url, array('remove_params' => 'all', 'add_param' => "node:$node")));
		exit;
	}
	
	
	
	
	$DATA['component'] = array_merge($temp, array('path' => $_SERVER['DOCUMENT_ROOT'].FP_COMPONENT_PATH.$temp['value'], 'settings' => datafetch_array('meta', D_MB_SETTINGS, $c)));
	
	$component_template = "{$DATA[component][path]}/admin";
	$component_route = component_route("{$DATA[component][value]}/admin", "show", array('name' => $DATA['component']['name']));

	$DATA['component']['route'] = "$component_template$component_route";

	if (file_exists("$component_template$component_route/init.php")) {

		$temp = $CURRENT->getPageById($node);	// !!! fix crumbs

		addCrumb(array(
			array(
			'name' => $temp['localname'] ? $temp['localname'] : $temp['name'],
				'path' => _transformURL($CURRENT->url, array('remove_params' => 'all', 'add_param' => "node:$node"))
			),
			array(
				'name' => "«{$DATA[component][name]}» ({$DATA[component][value]})", 
				'path' => _transformURL($CURRENT->url, array('remove_params' => 'all', 
					'add_param' => array('node' => $node, 'component' => $c)))
			)
		));

		if (($_res = include("$component_template$component_route/init.php")) === true) {	// end of component edition
			header("Location: "._transformURL($CURRENT->url, array('remove_params' => 'all', 'add_param' => "node:$node")));
			exit;
		} else if ($_res) {
			header("Location: $_res");
			exit;
		}
	}

} else {	// **** no such component ****

	header("Location: "._transformURL($CURRENT->url, array('remove_params' => 'all', 'add_param' => "node:$node")));
	exit;
}
